package com.stylefeng.guns.config;

import com.stylefeng.guns.common.persistence.model.Menu;
import com.stylefeng.guns.modular.system.dao.MenuDao;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.jdbc.ScriptRunner;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.io.IOException;
import java.nio.charset.Charset;
import java.sql.Connection;
import java.sql.DriverManager;

/**
 * Created by jondai on 2017/12/20.
 */
@Component
public class ScriptRunnerExecSql {

    @Value("${spring.datasource.url}")
    private String url;
    @Value("${spring.datasource.driver-class-name}")
    private String driver;
    @Value("${spring.datasource.username}")
    private String username;
    @Value("${spring.datasource.password}")
    private String password;

    @Resource
    MenuDao menuDao;

    public void init() throws IOException {
        //check db is init
        Menu menu = menuDao.selectById("596653fe545f4b5f9dbb35fe2e36d86a");
        if(menu == null){
            executeInitSql();
            executeUpdateSql();
        }else{
            if(!StringUtils.isEmpty(menu.getName()) && !"v1.1.1".equals(menu.getName())){
                executeUpdateSql();
            }
        }

//        String path=ScriptRunnerExecSql.class.getClassLoader().getResource("").getPath();
//        URLClassPath bootstrapClassPath = Launcher.getBootstrapClassPath();
//        URL url = ScriptRunnerExecSql.class.getClassLoader().getResource("db/guns_data.sql");
//        File dataSql = Resources.getResourceAsFile(ScriptRunnerExecSql.class.getClassLoader(),"/db/guns_data.sql");
//        File dataSql = new File(url.getPath());

//        System.out.println(dataSql.getAbsolutePath());
//        if(dataSql.exists()){
//            System.out.println("文件存在，执行data.sql");
//            executeInitSql();
//            System.out.println("执行data.sql完成，更改schema");
//            try {
//                //删除表结构以及数据
//                System.out.println("删除data.sql");
//                dataSql.delete();
//                File schemaFile = Resources.getResourceAsFile( ScriptRunnerExecSql.class.getClassLoader(),"/db/guns_schema.sql");
//                if(schemaFile.exists()){
//                    schemaFile.delete();
//                }
//                File schemaBakFile = Resources.getResourceAsFile(ScriptRunnerExecSql.class.getClassLoader(),"/db/guns_schema_bak.sql");
//                if(schemaBakFile.exists()){
//                    schemaBakFile.renameTo(schemaFile);
//                }
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//        }else{
//            System.out.println("guns_data文件不不不不不存在");
//        }

//        }
    }


    public void executeInitSql() {
        try {
            Class.forName(driver).newInstance();
            Connection conn = DriverManager.getConnection(url, username, password);
            ScriptRunner runner = new ScriptRunner(conn);
            Resources.setCharset(Charset.forName("Utf-8")); //设置字符集,不然中文乱码插入错误
            runner.setLogWriter(null);//设置是否输出日志
            runner.runScript(Resources.getResourceAsReader("db/guns_data.sql"));
            runner.closeConnection();
            conn.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void executeUpdateSql() {
        try {
            Class.forName(driver).newInstance();
            Connection conn = DriverManager.getConnection(url, username, password);
            ScriptRunner runner = new ScriptRunner(conn);
            Resources.setCharset(Charset.forName("Utf-8")); //设置字符集,不然中文乱码插入错误
            runner.setLogWriter(null);//设置是否输出日志
            runner.runScript(Resources.getResourceAsReader("db/update2-18.sql"));
            runner.closeConnection();
            conn.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }



}
